package com.dap.cms.governance.controller;

import com.dap.cms.governance.po.CmsCheckInfoPo;
import com.dap.cms.governance.service.CmsCheckInfoService;
import com.dap.dao.model.QueryResult;
import com.fesine.commons.entity.Result;
import com.fesine.commons.enums.ResultEnum;
import com.fesine.commons.util.ResultUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: t_cms_check_info表对应的控制层类
 * @author: Fesine
 * @createTime:2018/09/19
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2018/09/19
 */
@RestController
@RequestMapping("/v1")
public class CmsCheckInfoController {
    private static final Logger logger=LoggerFactory.getLogger(CmsCheckInfoController.class);
    @Autowired
    private CmsCheckInfoService cmsCheckInfoService;

    /**
     * 新增CmsCheckInfoPo
     * @param cmsCheckInfoPo
     * @return result
     */
  /*  @PostMapping("/cmsCheckInfo")
    public Result addCmsCheckInfo(CmsCheckInfoPo cmsCheckInfoPo){
        try{
            cmsCheckInfoService.save(cmsCheckInfoPo);
            return ResultUtils.success(ResultEnum.CREATED,null);
        }catch(Exception e){
            logger.error("新增失败",e);
            return ResultUtils.error(500,"新增失败");
        }

    }*/

    /**
     *申请单审核
     * @param applicationId
     * @param handingResult
     * @param otherMsg
     * @return
     */
    @PutMapping("/cmsCheckInfo")
    public Result updateCmsCheckInfo(@RequestParam Integer applicationId,
                                     @RequestParam Integer handingResult,
                                     @RequestParam String otherMsg){
        CmsCheckInfoPo cmsCheckInfoPo=new CmsCheckInfoPo();
        cmsCheckInfoPo.setHandingResult(0);
        cmsCheckInfoPo.setApplicationId(applicationId);
        cmsCheckInfoPo=cmsCheckInfoService.get(cmsCheckInfoPo);
        cmsCheckInfoPo.setHandingResult(handingResult);
        try{
            cmsCheckInfoService.examine(cmsCheckInfoPo,otherMsg);
            return ResultUtils.success(ResultEnum.CREATED,null);
        }catch(Exception e){
            logger.error("更新失败",e);
            return ResultUtils.error(500,"更新失败");
        }
    }

    /**
     * 删除CmsCheckInfoPo
     * @param id
     * @return result
     */
    @DeleteMapping("/cmsCheckInfo/{id}")
    public Result deleteCmsCheckInfo(@PathVariable("id") Integer id){
        CmsCheckInfoPo cmsCheckInfoPo=new CmsCheckInfoPo();
        cmsCheckInfoPo.setId(id);
        cmsCheckInfoService.delete(cmsCheckInfoPo);
        return ResultUtils.success(cmsCheckInfoPo);
    }

    /**
     * 查看CmsCheckInfoPo详情
     * @param id
     * @return result
     */
    @GetMapping("/cmsCheckInfo/{id}")
    public Result getCmsCheckInfo(@PathVariable("id") Integer id){
        CmsCheckInfoPo cmsCheckInfoPo=new CmsCheckInfoPo();
        cmsCheckInfoPo.setId(id);
        cmsCheckInfoPo=cmsCheckInfoService.get(cmsCheckInfoPo);
        return ResultUtils.success(cmsCheckInfoPo);
    }

    /**
     * 查询CmsCheckInfoPo列表
     * @param cmsCheckInfoPo
     * @return result
     */
    @GetMapping("/cmsCheckInfos")
    public Result listAll(CmsCheckInfoPo cmsCheckInfoPo){
        List<CmsCheckInfoPo> list=cmsCheckInfoService.listAll(cmsCheckInfoPo);
        return ResultUtils.success(list);
    }

    /**
     * 分页查询CmsCheckInfoPo列表
     * @param cmsCheckInfoPo
     * @param pageIndex
     * @param pageSize
     * @return result
     */
    @GetMapping("/cmsCheckInfos/page")
    public Result listPage(CmsCheckInfoPo cmsCheckInfoPo,@RequestParam(value = "pageIndex",
        required = false, defaultValue = "1")
            Integer pageIndex,@RequestParam(value = "pageSize", required = false, defaultValue =
        "10") Integer pageSize){
        QueryResult<CmsCheckInfoPo> result=cmsCheckInfoService.listPage(cmsCheckInfoPo,pageIndex,pageSize);
        long total=result.getTotalrecord();
        Map<String, Object> map=new HashMap(16);
        if(total > 0){
            map.put("totalRecord",total);
            map.put("resultList",result.getResultlist());
        }
        return ResultUtils.success(map);
    }

}

